import DataManager from "../../Script/DataManager";
import Loading from "../../Script/Loading";
import SDKUtil, { ApiType } from "../../Script/SDK/SDKUtil";
import Utils from "../../Script/Utils";
import ScrollViewCtrl from "./ScrollViewCtrl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class rankPanel extends cc.Component {

    @property(cc.Node)
    scrollView: cc.Node = null;

    @property(cc.Node)
    wxRankNode: cc.Node = null;

    @property(cc.Node)
    Rank: cc.Node = null;

    @property(cc.Node)
    backBtn: cc.Node = null;
    @property(cc.Node)
    groupRankBtn: cc.Node = null;

    @property(cc.Node)
    title: cc.Node = null;

    isFriendRank: boolean = true;
    shareId: string = "";

    _isFirst: boolean = true;

    onShareBtn() {
        SDKUtil.Instance.ShareApp(3);
    }

    setData(data) {
        this.isFriendRank = data.isFriendRank;
        if (data.id) this.shareId = data.id;
    }

    onLoad() {
        Utils.Instance.registerBtnEvent(this.backBtn, this.onCloseBtn, this);
        Utils.Instance.registerBtnEvent(this.groupRankBtn, this.onGroupRankBtn, this);
    }

    start() {
        this.init();
    }

    onGroupRankBtn() {
        SDKUtil.Instance.ShareeToGroup((id) => {
            this.isFriendRank = false;
            this.shareId = id;
            var subContex = this.node.getChildByName('subContextView');
            subContex.active = false;
            this.init();
        })
    }

    init() {
        SDKUtil.Instance.AdsShow(1);
        Utils.Instance.setLabelString(this.title, this.isFriendRank ? "好友排行榜" : "群排行榜");
        this.groupRankBtn.active = this.isFriendRank;
        let myscore = DataManager.Instance.getMaxScore();
        let myname = DataManager.Instance.getNickName();
        let imgUrl = DataManager.Instance.getAvatarUrl();
        let openId = DataManager.Instance.getOpenId();
        let time = DataManager.Instance.getRankReTime();
        if (myname) {
            if (myname.length > 9) {
                myname = myname.slice(0, 8) + "...";
            }
        }
        if (ApiType == 1 || ApiType == 2) {
            this.showOpenDataContext({
                score: myscore,
                name: myname,
                imgUrl: imgUrl,
                openid: openId,
                rankReTime: time
            });
        }
        return;
        this.wxRankNode.active = false;
        this.Rank.active = true;

        let infos = this.createUserData();
        //插入自己的分数

        for (let i = 0; i < infos.length; i++) {
            if (myscore > infos[i].score) {
                let my: any = {};
                my.name = myname;
                my.score = myscore;
                infos.splice(i, 0, my);
                break;
            }
        }
        cc.log(infos)
        this.scrollView.getComponent(ScrollViewCtrl).init(infos);
    }

    showOpenDataContext(myData) {
        this.scrollView.active = false;
        var subContex = this.node.getChildByName('subContextView');
        //subContex.active = true;
        let time = 0.5;
        if (!this._isFirst) time = 0;
        this.scheduleOnce(() => {
            if (subContex) {
                console.log("显示好友域");
                console.log(myData);
                subContex.getComponent(cc.SubContextView).enabled = true;
                subContex.getComponent(cc.SubContextView).reset();
                //subContex.getComponent(cc.SubContextView).updateSubContextViewport();
                if (ApiType == 0) return;
                let openDataContext = (ApiType == 1) ? window["wx"].getOpenDataContext() : window["tt"].getOpenDataContext();
                console.log("openDataContext", openDataContext);
                if (!openDataContext) return;
                console.log(window["wx"], ApiType);
                openDataContext.postMessage({
                    text: this.isFriendRank ? "friendRank" : "groupRank",
                    myData: JSON.stringify(myData)
                });
                this.scheduleOnce(() => {
                    let openDataContext = (ApiType == 1) ? window["wx"].getOpenDataContext() : window["tt"].getOpenDataContext();
                    console.log("openDataContext", openDataContext);
                    if (!openDataContext) return;
                    console.log(window["wx"], ApiType);
                    openDataContext.postMessage({
                        text: this.isFriendRank ? "friendRank" : "groupRank",
                        myData: JSON.stringify(myData)
                    });
                }, 0.5)
            }
            if (this._isFirst) {
                this._isFirst = false;
            }
        }, time)
    }

    createUserData() {

        let scores = [
            20, 28, 34, 46, 53,
            54, 55, 56, 58, 60,
            68, 69, 69, 70, 71,
            72, 75, 82, 90, 100
        ];

        let names = [
            '白茶与鹿',
            '忆挽青笙尽',
            '骑鹅撵飞机',
            '莣吥鋽の情',
            '盗版的兄弟',
            '狂剑メ血王',
            '打个酱油卖个萌',
            '时光吹老了少年',
            '违心爱',
            '飞天忍者猪',
            '紫静冰情',
            '漂亮口号',
            '666',
            '姬如炫蕊',
            '呐阳光 、刺眼',
            '私有物品',
            '筱愛',
            '明媚的憂傷',
            '苞米地蒙靣俠',
            '浮伤年华',
            '孟婆卖萌不卖汤',
            '柚子纪年',
            '独恋猫的鱼',
            'hhhh',
        ];


        let infos = [];

        for (let i = 0; i < scores.length; i++) {
            let data: any = {};
            data.name = names[i];
            data.score = scores[i];
            infos.push(data);
        }
        return infos;
    }


    onCloseBtn() {
        var subContex = this.node.getChildByName('subContextView');
        // setTimeout(() => {
        if (subContex) {
            subContex.getComponent(cc.SubContextView).enabled = false;
            subContex.getComponent(cc.SubContextView).reset();
            subContex.getComponent(cc.SubContextView).updateSubContextViewport();
        }
        SDKUtil.Instance.AdsHide();
        // if (this.node) this.node.destroy();
        if (this.node) this.node.active = false;
        // }, 200);
    }
}
